package com.zretc.users.controller;

import com.zretc.api.pojo.User;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.users.service.UsersService;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.zretc.security.entity.LoginUser;
import javax.annotation.Resource;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersService userService;

    @GetMapping("/userDetail")
    public RespResult getUserDetail(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser userInfo = (LoginUser) authentication.getPrincipal();
        return R.success(userService.getUserById(userInfo.getUser().getUserId()));
    }

    @PostMapping("/updateNicknameOrPhone")
    public RespResult updateNicknameOrPhone(@RequestBody User newInfo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser userInfo = (LoginUser) authentication.getPrincipal();
        User user = userInfo.getUser();
        return userService.changeNicknameOrPhone(user, newInfo);
    }

    // UserFeignClient
    @GetMapping("/getUserById")
    public User getUserInfoById(@RequestParam("userId") Integer userId){
        System.out.println("UserController-------------------------------------------------------------------------"); // test
        System.out.println(userId); // test

        return userService.getUserById(userId);
    }

    @GetMapping("/getUserByUsername")
    public User getUserInfoByUsername(@RequestParam("userName") String userName){
        return userService.getUserByUsername(userName);
    }

    @GetMapping("/getUserPoint")
    public Integer getPoint(@RequestParam("userId") Integer userId){
        return userService.getPoint(userId);
    }

    @PostMapping("/updatePointOrReputation")
    public RespResult updatePointOrReputation(@RequestBody User user){
        return userService.changePointOrRepu(user);
    }

    @GetMapping("/getUserReputation")
    public Integer getReputation(@RequestParam("userId") Integer userId){
        return userService.getReputation(userId);
    }
}
